package com.cjm.algorithm;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(Integer val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public static TreeNode generatorTreeNode(Integer[] nums) {
        if (nums.length < 1) {
            return null;
        }
        TreeNode root = new TreeNode();
        Queue<TreeNode> queue = new ArrayBlockingQueue<TreeNode>(65535);
        queue.add(root);
        int index = 0;
        while (index < nums.length) {
            TreeNode cur = queue.poll();
            Integer val = nums[index];
            if (val == null) {
                cur = null;
                index++;
                continue;
            }
            cur.val = val;
            TreeNode right = new TreeNode();
            TreeNode left = new TreeNode();
            cur.left = left;
            cur.right = right;
            queue.add(left);
            queue.add(right);
            index++;
        }

        deal(root);
        return root;
    }

    private static void deal(TreeNode root) {
        TreeNode left = root.left;
        TreeNode right = root.right;
        if (left.val == 0) {
            root.left = null;
        } else {
            deal(left);
        }
        if (right.val == 0) {
            root.right = null;
        } else {
            deal(right);
        }
    }
}
